package com.yimi.appstore.adapter;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.yimi.appstore.R;
import com.yimi.appstore.db.dao.AppInfoEntity;
import com.yimi.appstore.util.FileUtils;
import com.yimi.appstore.util.Util;

public class AppInstalledAdapter  extends MyAdapter{

    public AppInstalledAdapter(Context context, List<HashMap<String, Object>> listData) {
        super(context, listData);
    }
    
    class ViewHolder{
        ImageView app_icon;
        CheckBox app_checkbox;
        TextView app_name,app_version,app_size,app_uninstall;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final HashMap<String, Object> item = listData.get(position);
        View v = showLoadingItem(item);
        if(v != null)
            return v;
        v = convertView ;
        ViewHolder vh = null ;
        if(v == null || v.getTag() == null) {
            v = lInflater.inflate(R.layout.apk_installed_item, null);
            vh = new ViewHolder();
            vh.app_checkbox = (CheckBox) v.findViewById(R.id.ck_selected);
            vh.app_icon = (ImageView) v.findViewById(R.id.iv_icon);
            vh.app_name = (TextView) v.findViewById(R.id.tv_name);
            vh.app_version = (TextView) v.findViewById(R.id.tv_version);
            vh.app_size = (TextView) v.findViewById(R.id.tv_size);
            vh.app_uninstall = (TextView) v.findViewById(R.id.tv_unistall);
            v.setTag(vh);
        } else {
           vh =(ViewHolder) v.getTag();
        }
        if(item.containsKey(AppInfoEntity.APP_ICON)){
        	vh.app_icon.setImageDrawable((Drawable)item.get(AppInfoEntity.APP_ICON));
        } else {
        	vh.app_icon.setImageResource(R.drawable.app_deafault);
		}
        if(item.containsKey(AppInfoEntity.APP_NAME))
            vh.app_name.setText((String)item.get(AppInfoEntity.APP_NAME));
        if(item.containsKey(AppInfoEntity.APP_VERSION_NAME))
            vh.app_version.setText("版本:"+(String)item.get(AppInfoEntity.APP_VERSION_NAME));
        if((Integer)item.get(AppInfoEntity.APP_SIZE)!= 0){
        	vh.app_size.setText(
        			FileUtils.convertStorage((Integer)item.get(AppInfoEntity.APP_SIZE)));
        }else {
        	vh.app_size.setText("正在计算");
		}
        vh.app_uninstall.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Util.unstallApk(context, (String)item.get(AppInfoEntity.APP_PACKAGE_NAME));
			}
		});
        vh.app_checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				View v = lInflater.inflate(R.layout.apk_installed_item, null);
			}
		});
        return v;
    }

}
